package com.lxl.securitypermission.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lxl.securitypermission.constant.HttpCode;
import com.lxl.securitypermission.constant.StatusCode;
import com.lxl.securitypermission.controller.vo.SimplePermissionVo;
import com.lxl.securitypermission.dto.ResponseResult;
import com.lxl.securitypermission.dto.ResponseResultPage;
import com.lxl.securitypermission.entity.SPermission;
import com.lxl.securitypermission.entity.SRole;
import com.lxl.securitypermission.mapper.SPermissionMapper;
import com.lxl.securitypermission.service.PermissionService;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PermissionServiceImpl implements PermissionService {

  @Autowired
  private SPermissionMapper sPermissionMapper;

  @Override
  public ResponseResultPage getPermissionList(String search, int page, int limit) {
    Page pages = PageHelper.startPage(page, limit);
    List<SPermission> result = sPermissionMapper.getList(search);
    return new ResponseResultPage(HttpCode.OK, page, limit, pages.getTotal(), result);
  }

  @Override
  public ResponseResult sw(Integer id,Integer status) {
    sPermissionMapper.sw(id,status);
    return new ResponseResult(HttpCode.OK,"更改成功！");
  }

  @Override
  public ResponseResult delete(Integer id) {
    sPermissionMapper.sw(id,2);
    return new ResponseResult(HttpCode.OK,"删除成功！");
  }

  @Override
  public ResponseResult edit(Integer id, String pattern, String action) {
    SPermission sPermission=new SPermission();
    sPermission.setId(id);
    sPermission.setAction(action);
    sPermission.setPattern(pattern);
    sPermission.setUpdateTime(new Date());
    sPermissionMapper.updateByPrimaryKeySelective(sPermission);
    return new ResponseResult(HttpCode.OK,"更新成功！");
  }

  @Override
  public ResponseResult add(String pattern, String action) {
    SPermission sPermission=new SPermission();
    sPermission.setAction(action);
    sPermission.setPattern(pattern);
    sPermission.setCreateTime(new Date());
    sPermission.setStatus(StatusCode.VALID);
    sPermissionMapper.insertSelective(sPermission);
    return new ResponseResult(HttpCode.OK,"添加成功！");
  }

  @Override
  public List<SimplePermissionVo> getSimplePermissionVo(int status) {
    return sPermissionMapper.getSimplePermissionVo(status);
  }
}
